home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Development / 3DMF parser / 0.9 version / MFFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-21  |  6.3 KB  |  233 lines  |  [TEXT/MPS ]

  1. /*==============================================================================
  2.  *
  3.  *    File:        MFFILE.C
  4.  *
  5.  *    Function:    Miscellaneous metafile routines.
  6.  *
  7.  *    Author(s):    Rick Wong (RWW), Duet Development Corp.
  8.  *
  9.  *    Copyright:    (c) 1995 by Apple Computer, Inc., all rights reserved.
  10.  *
  11.  *    Change History (most recent first):
  12.  *        Fabio    Changed file name to 8 characters
  13.  *        F3F_RWW    File created.
  14.  *==============================================================================
  15.  */
  16.  
  17. #include "MFFILE.H"
  18.  
  19. #include "MF3D.H"
  20. #include "MFERRORS.H"
  21. #include "MFOBJCTS.H"
  22. #include "MFTYPES.H"
  23. #include "MFASSERT.H"
  24. #include "MFINTOBJ.H"
  25. #include "MFMEMORY.H"
  26. #include "MFPRIMTV.H"
  27. #include "MFTEXTST.H"
  28. #include "MFTEXTUT.H"
  29. #include "MFTEXTWR.H"
  30.  
  31. /*==============================================================================
  32.  *    MF3D_TypeObjWrite
  33.  *
  34.  *    If this is a new user-defined type, write a Type object
  35.  *==============================================================================
  36.  */
  37. MF3DErr
  38. MF3D_TypeObjWrite(
  39.     MF3D_FilePtr        inMetafilePtr,
  40.     MF3DVoidObjPtr        inMF3DObjPtr)
  41. {
  42.     MF3DUnknownObjPtr    object;
  43.     MF3DInt32            objType;
  44.     MF3DBoolean            foundYet;
  45.     MF3DErr                result;
  46.  
  47.     object = (MF3DUnknownObjPtr)inMF3DObjPtr;
  48.     MFASSERT(object->objectType == kMF3DObjUnknownType);
  49.  
  50.     MFASSERT(sizeof(objType) == sizeof(object->realObjectType));
  51.     objType = (MF3DInt32)(object->realObjectType);
  52.  
  53.     result = kMF3DNoErr;
  54.  
  55.     if (objType <= kMF3DMinimumTypeSeed)
  56.         result = kMF3DErrIllegalUserObjectType;
  57.  
  58.     /* Is it a user-defined type? */
  59.     if (result == kMF3DNoErr && objType < 0)
  60.     {    MF3DUns32            numTypes;
  61.         MF3D_TypeListPtr    typeListPtr;
  62.  
  63.         typeListPtr = inMetafilePtr->typeTable.types;
  64.         foundYet = kMF3DBooleanFalse;
  65.         for (numTypes = inMetafilePtr->typeTable.nTypes;
  66.                 foundYet == kMF3DBooleanFalse && numTypes > 0 &&
  67.                 result == kMF3DNoErr;
  68.                 --numTypes, ++typeListPtr)
  69.         {    if (objType == typeListPtr->number)
  70.             {    if (MF3D_CmpStrInsensitive(typeListPtr->name,
  71.                         (const char *)object->realObjectName) == 0)
  72.                 {    foundYet = kMF3DBooleanTrue;    /* type already in table */
  73.                 }
  74.                 else
  75.                 {    result = kMF3DErrIllegalUserObjectType;    /* type with    */
  76.                                         /* different name already in table    */
  77.                 }
  78.             }
  79.         }
  80.     }
  81.  
  82.     if (result == kMF3DNoErr && foundYet == kMF3DBooleanFalse)
  83.     {    MF3D_TypeListPtr    tempPtr;
  84.         MF3DUns32            numTypes;
  85.  
  86.         /* Add this type to the table */
  87.         ++inMetafilePtr->typeTable.nTypes;
  88.         numTypes = inMetafilePtr->typeTable.nTypes;
  89.         tempPtr = MF3D_Realloc(inMetafilePtr->typeTable.types,
  90.                 numTypes * sizeof(*inMetafilePtr->typeTable.types));
  91.         if (tempPtr == NULL)
  92.             result = kMF3DErrOutOfMemory;
  93.         else
  94.         {    inMetafilePtr->typeTable.types = tempPtr;
  95.             inMetafilePtr->typeTable.types[numTypes - 1].number = objType;
  96.             inMetafilePtr->typeTable.types[numTypes - 1].name =
  97.                     MF3D_DuplicateCString(object->realObjectName);
  98.  
  99.             if (objType < inMetafilePtr->tocStuff.typeSeed)
  100.                 inMetafilePtr->tocStuff.typeSeed = objType - 1;
  101.         }
  102.  
  103.         /* Output the Type object */
  104.         if (result == kMF3DNoErr)
  105.         {    MF3DVoidObj            fakeObj;
  106.             MF3D_ObjStuffPtr    objStuff;
  107.  
  108.             fakeObj.objectType = kMF3DObjType;
  109.             result = MF3D_BeginWrite(inMetafilePtr, &fakeObj, &objStuff);
  110.  
  111.             if (result == kMF3DNoErr)
  112.                 result = (*objStuff->writer) (inMetafilePtr, inMF3DObjPtr);
  113.             
  114.             if (result == kMF3DNoErr)
  115.                 result = MF3D_EndWrite(inMetafilePtr, &fakeObj);
  116.  
  117.             if (result == kMF3DNoErr)
  118.                 MF3D_WriteNewLine(inMetafilePtr);
  119.         }
  120.     }
  121.  
  122.     return result;
  123. }
  124.  
  125. /*==============================================================================
  126.  *    MF3D_CloseReadBuffer
  127.  *
  128.  *    Verify that ReadBuffer is completely popped
  129.  *==============================================================================
  130.  */
  131. MF3DErr
  132. MF3D_CloseReadBuffer(
  133.     MF3D_FilePtr        inMetafilePtr)
  134. {
  135.     MF3DErr        result;
  136.  
  137.     result = kMF3DNoErr;
  138.  
  139.     if (inMetafilePtr->readBuffer.buf != NULL)
  140.     {    MF3D_Free(inMetafilePtr->readBuffer.buf);
  141.         result = kMF3DErrNotEnoughEndGroups;
  142.  
  143.         while (inMetafilePtr->readBuffer.saveSize != NULL)
  144.         {    MF3D_SaveBufferPtr    sizeStack;
  145.     
  146.             sizeStack = inMetafilePtr->readBuffer.saveSize;
  147.             inMetafilePtr->readBuffer.bufSize = sizeStack->bufSize;
  148.             inMetafilePtr->readBuffer.saveSize = sizeStack->next;
  149.             MF3D_Free(sizeStack);
  150.         }
  151.     }
  152.  
  153.     return result;
  154. }
  155.  
  156. /*==============================================================================
  157.  *    MF3D_BackpatchTOCLocation
  158.  *
  159.  *    Patch the location of the TOC into the metafile header
  160.  *==============================================================================
  161.  */
  162. MF3DErr
  163. MF3D_BackpatchTOCLocation(
  164.     MF3D_FilePtr        inMF3DFilePtr)
  165. {
  166.     MF3DErr        result, seekResult;
  167.  
  168.     result = kMF3DNoErr;
  169.  
  170.     /* Backpatch TOC location only for binary files */
  171.     if (!MF3DIsTextFormat(inMF3DFilePtr->dataFormat))
  172.     {    MF3DBinaryFilePosition    curLocation;
  173.  
  174.         result = MF3DTellPosition(inMF3DFilePtr, &curLocation);
  175.  
  176.         if (result == kMF3DNoErr)
  177.         {    result = MF3DSeekPosition(inMF3DFilePtr,
  178.                     inMF3DFilePtr->tocLocation);
  179.         }
  180.  
  181.         if (result == kMF3DNoErr)
  182.             result = MF3D_Uns64Write(inMF3DFilePtr, curLocation);
  183.  
  184.         seekResult = MF3DSeekPosition(inMF3DFilePtr, curLocation);
  185.         if (result == kMF3DNoErr)
  186.             result = seekResult;
  187.     }
  188.     else
  189.     {    MFASSERT(inMF3DFilePtr->tocStuff.tocLabelName != NULL);
  190.         MF3D_ValidateWriteSize(inMF3DFilePtr, strlen(kMF3D_LabelCharStr " ") +
  191.                 strlen(inMF3DFilePtr->tocStuff.tocLabelName));
  192.         result = MF3D_OutputText(inMF3DFilePtr, "%s" kMF3D_LabelCharStr " ",
  193.                 inMF3DFilePtr->tocStuff.tocLabelName);
  194.         MF3D_WriteNewLine(inMF3DFilePtr);
  195.  
  196.         MF3D_Free(inMF3DFilePtr->tocStuff.tocLabelName);
  197.         inMF3DFilePtr->tocStuff.tocLabelName = NULL;
  198.     }
  199.  
  200.     return result;
  201. }
  202.  
  203. /*==============================================================================
  204.  *    MF3D_DisposeTOCStuff
  205.  *
  206.  *    Dispose Table of Contents stuff
  207.  *==============================================================================
  208.  */
  209. MF3DErr
  210. MF3D_DisposeTOCStuff(
  211.     MF3D_FilePtr        inMetafilePtr)
  212. {
  213.     MF3DUns32                entriesLeft;
  214.     MF3D_TOCReferencePtr    curRefPtr;
  215.  
  216.     entriesLeft = inMetafilePtr->tocStuff.numReferences;
  217.  
  218.     if (MF3DIsTextFormat(inMetafilePtr->dataFormat))
  219.     {    curRefPtr = inMetafilePtr->tocStuff.references;
  220.         MFASSERT(curRefPtr != NULL || entriesLeft == 0);
  221.         for ( ; entriesLeft > 0; --entriesLeft, ++curRefPtr)
  222.             MF3D_Free(curRefPtr->ref.name);
  223.     }
  224.  
  225.     MF3D_Free(inMetafilePtr->tocStuff.references);
  226.     /* If there was no Table of Contents, tocLabelName has not been
  227.      * disposed.
  228.      */
  229.     MF3D_Free(inMetafilePtr->tocStuff.tocLabelName);
  230.  
  231.     return kMF3DNoErr;
  232. }
  233.